<?php


namespace App\Logic;

use App\Exceptions\ApiException;
use App\Models\OptionModel;
use App\Models\PaperModel;
use App\Models\StudentModel;
use App\Utils\Singleton;
use Illuminate\Http\Request;
use Illuminate\Http\Response;


class StudentLogic
{
    use Singleton;

    public function add(Request $request)
    {
        $response = Response::create();
        $stu_name = $request->input('stu_name');
        $phone = $request->input('phone');
        $subject_id = $request->input('subject_id');

        $insertData = ['stu_name' => $stu_name, 'phone' => $phone,'subject_id' => $subject_id]; //要插入的考生信息

        if (!empty($stu_name) || !empty($phone)) {  //当输入不为空时
            if (preg_match('/^[\x7f-\xff]+$/',
                    $stu_name) && preg_match('/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/', $phone)) {     //正则表达式



                $result_stu_id = PaperModel::getInstance()->getOne(['stu_id'], ['phone' => $phone]);

                if ($result_stu_id) {   //当该考生存在时

                    $result_subject_id = PaperModel::getInstance()->getOne(["subject_id"],
                        ['stu_id' => $result_stu_id->stu_id]);//查找该考生是否考过该科目

                    if (null != $result_subject_id->subject_id && !is_array($result_subject_id->subject_id)) {
                        $response->exception = new ApiException(1, '该考生已经考过本岗位!');

                    } else {
                        return $data = [
                            'stu_id' => $result_stu_id->stu_id
                        ];
                    }
                } else {
                    return $data = [
                        'stu_id' => PaperModel::getInstance()->insertData($insertData)    //插入考生信息到数据库
                    ];
                }
            } else {
                $response->exception = new ApiException(1, '姓名或者手机号格式错误！');
            }
        } else {
            $response->exception = new ApiException(1, '考生信息不能为空！');
        }
        return [];
    }
    public function login(Request $request,Response $response){
        $stu_name = $request->input('stu_name');
        $phone = $request->input('phone');
        $subject_id = $request->input('subject_id');
        $subject_id = decrypt($subject_id);
        $insertData = ['stu_name' => $stu_name, 'phone' => $phone,'subject_id' => $subject_id];
        if(empty($stu_name) || empty($phone)){
            $response->exception = new ApiException(0, '请输入！');
            return $response;
        }
        if (!empty(PaperModel::getInstance()->getList('stu_id',
            ['phone' => $phone,'subject_id' => $subject_id]))){
            $response->exception = new ApiException(-1, '考生已登录！');
            return $response;
        }else{
            PaperModel::getInstance()->insertData($insertData);
            $stu_id=objectToArray(PaperModel::getInstance()->getList('stu_id',
                ['phone' => $phone,'subject_id' => $subject_id]))[0];
            if (!empty($stu_id)){
                $list['stu_id']=$stu_id['stu_id'];
                return $list;
            }else{
                $response->exception = new ApiException(-2, '状态异常！');
                return $response;
            }
        }
    }
}

